package product;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.SwingConstants;

public class ProductTableAction extends JFrame {

	ArrayList<ProductData> pro;
	Vector<String> vec;
	DefaultTableModel model;
	int count;
	int index= -1;//마우스클릭된 정보가 ArrayList어디에 위치해있는지를 찾아서 저장
	DecimalFormat df = new DecimalFormat("###,###,###.##");
	
	private JPanel contentPane;
	private JLabel label;
	private JTextField proCode;
	private JLabel label_1;
	private JTextField proName;
	private JTextField proPrice;
	private JLabel label_2;
	private JLabel label_3;
	private JTextField proEa;
	private JTextField proTot;
	private JLabel label_4;
	private JButton button;
	private JButton button_1;
	private JLabel status;

	/**
	 * Create the frame.
	 */
	public ProductTableAction(ArrayList<ProductData> p, Vector<String> v,
			DefaultTableModel m) {
		this.pro = p;
		this.vec = v;
		this.model = m;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 234, 272);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getLabel());
		contentPane.add(getProCode());
		contentPane.add(getLabel_1());
		contentPane.add(getProName());
		contentPane.add(getProPrice());
		contentPane.add(getLabel_2());
		contentPane.add(getLabel_3());
		contentPane.add(getProEa());
		contentPane.add(getProTot());
		contentPane.add(getLabel_4());
		contentPane.add(getButton());
		contentPane.add(getButton_1());
		contentPane.add(getStatus());
		
		
		proCode.setText(vec.get(0));
		proName.setText(vec.get(1));
		String str = df.format(Double.parseDouble(vec.get(2)));
		proPrice.setText(str+"원");
		proEa.setText(vec.get(3));
		String str2 = df.format(Double.parseDouble(vec.get(4)));
		proTot.setText(str2+"원");
		count = Integer.parseInt(vec.get(6));
		for(int i=0; i<pro.size();i++){
			if(pro.get(i).getCount()==count){
				index = i;//전달 받은 정보안에있는 생산번호가 ArrayList몇번째에 있는지 찾음
			}
		}
	}
	
	public String replace(String s) {
		String temp = s.replace(",","");
		String temp2 = temp.replace("원","");
		return temp2;
	}
	
	public void compute(){
		try{
		int ea = Integer.parseInt(proEa.getText());
		int price = Integer.parseInt(replace(proPrice.getText()));
		int tot = ea*price;
		String str = df.format((double)tot);
		proTot.setText(str+"원");
		}catch(Exception e){
			System.out.println("error");
		}
	}
	
	public void modi(){//수정 매서드
		try{
			String code = proCode.getText();
			String name = proName.getText();
			int price = Integer.parseInt(replace(proPrice.getText()));
			int ea = Integer.parseInt(proEa.getText());
			int tot = Integer.parseInt(replace(proTot.getText()));
			ProductData data = new ProductData(code, name, price, ea, tot,count);
			pro.set(index, data);//해당 index의 정보를 data로 수정
			try {//수정이되면서 파일을 저장
				FileOutputStream fw = new FileOutputStream("ProductList.dat");
				ObjectOutputStream bw = new ObjectOutputStream(fw);
				bw.writeObject(pro);//저장
				bw.close();
				fw.close();
				System.out.println("수정성공");
			} catch (Exception e) {
				System.out.println("error");
			}
			status.setText(code + "수정됨");
			String str = df.format((double)price);
			String str2 = df.format((double)tot);
			model.setValueAt(String.valueOf(str)+"원", index, 2);// 변경할 내용, 행, 열
			model.setValueAt(String.valueOf(ea), index, 3);
			model.setValueAt(String.valueOf(str2)+"원", index, 4);
		}catch(Exception e){
			JOptionPane.showMessageDialog(null, "숫자를 입력하세요.");
		}
	}
	
	public void delete(){
		int yn = JOptionPane.showConfirmDialog(null, "삭제하시겠습니까?");
		if(yn == JOptionPane.NO_OPTION) return;
		else if(yn == JOptionPane.YES_NO_OPTION){
			pro.remove(index); //ArrayList에서 해당 index를 삭제
			try {
				FileOutputStream fw = new FileOutputStream("ProductList.dat");
				ObjectOutputStream bw = new ObjectOutputStream(fw);
				bw.writeObject(pro);//파일 저장
				bw.close();
				fw.close();
				System.out.println("삭제성공");
				model.removeRow(index);//테이블에서 삭제
				dispose();//수정&삭제창 닫기
			} catch (Exception e) {
				System.out.println("error");
			}
		}
	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("제품코드");
			label.setBounds(12, 13, 57, 15);
		}
		return label;
	}
	private JTextField getProCode() {
		if (proCode == null) {
			proCode = new JTextField();
			proCode.setEditable(false);
			proCode.setColumns(10);
			proCode.setBounds(81, 10, 116, 21);
		}
		return proCode;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("제품명");
			label_1.setBounds(12, 41, 57, 15);
		}
		return label_1;
	}
	private JTextField getProName() {
		if (proName == null) {
			proName = new JTextField();
			proName.setEditable(false);
			proName.setColumns(10);
			proName.setBounds(81, 38, 116, 21);
		}
		return proName;
	}
	private JTextField getProPrice() {
		if (proPrice == null) {
			proPrice = new JTextField();
			proPrice.setHorizontalAlignment(SwingConstants.RIGHT);
			proPrice.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent arg0) {
					compute();// 계산 매서드
				}
			});
			proPrice.setColumns(10);
			proPrice.setBounds(81, 69, 116, 21);
		}
		return proPrice;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("단가");
			label_2.setBounds(12, 72, 57, 15);
		}
		return label_2;
	}
	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("수량");
			label_3.setBounds(12, 100, 57, 15);
		}
		return label_3;
	}
	private JTextField getProEa() {
		if (proEa == null) {
			proEa = new JTextField();
			proEa.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent arg0) {
					compute();//계산 매서드
				}
			});
			proEa.setColumns(10);
			proEa.setBounds(81, 97, 116, 21);
		}
		return proEa;
	}
	private JTextField getProTot() {
		if (proTot == null) {
			proTot = new JTextField();
			proTot.setHorizontalAlignment(SwingConstants.RIGHT);
			proTot.setEditable(false);
			proTot.setColumns(10);
			proTot.setBounds(81, 128, 116, 21);
		}
		return proTot;
	}
	private JLabel getLabel_4() {
		if (label_4 == null) {
			label_4 = new JLabel("금액");
			label_4.setBounds(12, 131, 57, 15);
		}
		return label_4;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("수정");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					modi();//수정 매서드
				}
			});
			button.setBounds(22, 159, 78, 23);
		}
		return button;
	}
	private JButton getButton_1() {
		if (button_1 == null) {
			button_1 = new JButton("삭제");
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					delete();//삭제 매서드
				}
			});
			button_1.setBounds(119, 159, 78, 23);
		}
		return button_1;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel(" ");
			status.setOpaque(true);
			status.setBackground(Color.CYAN);
			status.setBounds(12, 192, 185, 15);
		}
		return status;
	}
}
